package com.cct.digitalarchapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.dao.ArchApplicationMapper;
import com.cct.digitalarchapi.dao.DigitalArchMainMapper;
import com.cct.digitalarchapi.dao.MessageMapper;
import com.cct.digitalarchapi.pojo.ArchApplication;
import com.cct.digitalarchapi.pojo.ArchCategory;
import com.cct.digitalarchapi.pojo.DigitalArchMain;
import com.cct.digitalarchapi.pojo.Message;
import com.cct.digitalarchapi.pojo.vo.ArchApplicationVo;
import com.cct.digitalarchapi.service.ArchApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @FileName ArchCategoryServiceImpl
 * @Description 实现类
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@Slf4j
@Service
public class ArchApplicationServiceImpl implements ArchApplicationService {

    @Autowired
    private ArchApplicationMapper archApplicationMapper;
    @Autowired
    private DigitalArchMainMapper digitalArchMainMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public Response addArchApplication(ArchApplication archApplication) {
        try{
            int archMainId=archApplication.getArchMainId();
            String staffCode4App=archApplication.getStaffCode4App();
            int appState=0;
            int num=archApplicationMapper.qryHaveApply(archMainId,staffCode4App,appState);
            if(num>=1){
                return Response.bad("您发起的该档案借阅已在审批中！");
            }
            int num2=archApplicationMapper.qryHaveApply(archMainId,staffCode4App,5);
            if(num2>=1){
                return Response.bad("您借阅的档案已有审批通过的！");
            }
            archApplication.setAppState(0);
            archApplicationMapper.addArchApplication(archApplication);
            //记录消息
            DigitalArchMain main=digitalArchMainMapper.qryArchMainById(archMainId);
            Message message=new Message();
            message.setType(3);
            message.setMessState(0);
            message.setOperator("all");
            message.setMessContent("您有待审批的档案借阅，档案名称为："+ main.getArchTitle()+" ，档案借阅人工号："+staffCode4App);
            messageMapper.addMessge(message);
            return Response.success("");
        }catch (Exception e){
            e.printStackTrace();
            log.error("发起借阅申请失败：{}",e.getMessage());
            return Response.error("发起借阅申请失败");
        }
    }

    @Override
    public Response qryArchApplication(String type, String staffCode,int pageNum,int pageSize,int appState,String archTitle) {
        try{
            Page<ArchApplicationVo> page=new Page<>(pageNum,pageSize);
            if("1".equals(type)){
               archApplicationMapper.qryAllApplication(page,appState,archTitle);
            }else if("2".equals(type)){
                archApplicationMapper.qryApplicationByStaff(page,staffCode,appState,archTitle);
            }else{
                return Response.bad("传入的type不正确");
            }
            for (ArchApplicationVo record : page.getRecords()) {
                if(record.getAppState()==5) {
                    Date now = new Date();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(record.getEstBorrowDay());
                    calendar.add(Calendar.DAY_OF_MONTH, record.getDuring());
                    if (now.getTime() > calendar.getTime().getTime()) {
                        record.setAppState(7);
                        archApplicationMapper.updateState(record.getRecid(), 7);
                    }

                }
            }
            return Response.success(page);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询借阅的档案失败：{}",e.getMessage());
            return Response.error("查询借阅的档案失败");
        }
    }

    @Override
    public Response approveApplication(ArchApplication archApplication) {
        try{
            Date estBorrowDay = new Date();
            //审核通过借阅次数加1
            if(archApplication.getAppState()==5) {
                digitalArchMainMapper.updateDownCount(archApplication.getArchMainId());
            }
            archApplication.setEstBorrowDay(estBorrowDay);
            archApplicationMapper.approveApplication(archApplication);
            //记录消息
            ArchApplication application=archApplicationMapper.getApplyById(archApplication.getRecid());
            DigitalArchMain main=digitalArchMainMapper.qryArchMainById(archApplication.getArchMainId());
            Message message=new Message();
            message.setType(2);
            message.setMessState(0);
            message.setOperator(application.getStaffCode4App());
            message.setMessContent("您借阅的档案名称为"+main.getArchTitle()+"的档案已审批通过");
            messageMapper.addMessge(message);
            return Response.success("");
        }catch (Exception e){
            e.printStackTrace();
            log.error("档案借阅审批失败：{}",e.getMessage());
            return Response.error("档案借阅审批失败");
        }
    }
}
